

package com.mt.restaurant.controller.material;

import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.mt.restaurant.dto.material.MaterialEditDto;
import com.mt.restaurant.entity.material.Material;
import com.mt.restaurant.service.material.MaterialService;


@Api(tags = "材料")
@RestController
@RequestMapping("/api/restaurant/material/Material")
@CrossOrigin(allowCredentials = "true")
public class MaterialController {
    private static Logger logger = LoggerFactory.getLogger(MaterialController.class);


    @Autowired
    private MaterialService materialService;

    /**
     * 根据分页参数查询材料集合
     *
     * @param pageDTO 分页条件
     */
//    @PreAuthorize("hasAuthority('restaurant:material:Material:view')")
    @ApiOperation("分页查询材料")
    @ApiPageParam
    @PostMapping("/findMaterials")
    public PageResultDTO findMaterials(@RequestBody PageDTO pageDTO) {
        return this.materialService.findMaterials(pageDTO);
    }

    /**
     * 根据id查询指定的材料
     *
     * @param materialId Id
     */
    @PreAuthorize("hasAuthority('restaurant:material:Material:view')")
    @ApiOperation("根据id查询材料")
    @ApiPageParam
    @PostMapping("/findMaterial")
    public Material findMaterial(@RequestParam Long materialId) {
        return this.materialService.findMaterial(materialId);
    }

    /**
     * 根据ID查询指定的材料(包含外键名称)
     *
     * @param materialId Id
     */
    @PreAuthorize("hasAuthority('restaurant:material:Material:view')")
    @ApiOperation("根据ID查询指定的材料(包含外键名称)")
    @PostMapping("/findMaterialForView")
    public Material findMaterialForView(@RequestParam Long materialId) {
        return this.materialService.findMaterialWithForeignName(materialId);
    }

    /**
     * 根据ID查询指定的材料(包含材料和外键名称)
     *
     * @param materialId Id
     */
    @PreAuthorize("hasAuthority('restaurant:material:Material:view')")
    @ApiOperation("根据ID查询指定的材料(包含材料和外键名称)")
    @PostMapping("/findMaterialForEdit")
    public MaterialEditDto findMaterialForEdit(@RequestParam Long materialId) {
        MaterialEditDto materialEditDto = new MaterialEditDto();
        materialEditDto.setMaterial(this.materialService.findMaterialWithForeignName(materialId));

        this.prepareMaterialEditDto(materialEditDto);

        return materialEditDto;
    }

    /**
     * 根据ID查询指定的材料(只提取ID 和 Name)
     *
     * @param materialId Id
     */
    @PreAuthorize("hasAuthority('restaurant:material:Material:view')")
    @ApiOperation("根据ID查询指定的材料(只提取ID 和 Name)")
    @PostMapping("/findMaterialsWithIdNameById")
    public Material findMaterialsWithIdNameById(@RequestParam Long materialId) {
        return this.materialService.findMaterialsWithIdNameById(materialId);
    }

    /**
     * 根据名称查询材料集合(只提取ID 和 Name)
     *
     * @param materialName 名称
     */
    @PreAuthorize("hasAuthority('restaurant:material:Material:view')")
    @ApiOperation("根据名称查询材料集合(只提取ID 和 Name)")
    @PostMapping("/findMaterialsWithIdNameByName")
    public List<Material> findMaterialsWithIdNameByName(@RequestParam String materialName) {
        return this.materialService.findMaterialsWithIdNameByName(materialName);
    }


    /**
     * 创建新的材料
     */
    @PreAuthorize("hasAuthority('restaurant:material:Material:add')")
    @ApiOperation("创建新的材料")
    @PostMapping("/createMaterial")
    public MaterialEditDto createMaterial() {
        MaterialEditDto materialEditDto = new MaterialEditDto();
        materialEditDto.setMaterial(new Material());

        this.prepareMaterialEditDto(materialEditDto);
        return materialEditDto;
    }

    private void prepareMaterialEditDto(MaterialEditDto materialEditDto) {
    }

    /**
     * 新增保存材料
     *
     * @param material 实体对象
     */
    @PreAuthorize("hasAuthority('restaurant:material:Material:add')")
    @ApiOperation("新增保存材料")
    @PostMapping("/saveMaterial")
    public Material saveMaterial(@RequestBody Material material) {
        return this.materialService.saveMaterial(material);
    }

    /**
     * 修改保存材料
     *
     * @param material 实体对象
     */
    @PreAuthorize("hasAuthority('restaurant:material:Material:update')")
    @ApiOperation("修改保存材料")
    @PostMapping("/updateMaterial")
    public Material updateMaterial(@RequestBody Material material) {
        return this.materialService.updateMaterial(material);
    }

    /**
     * 根据ID删除材料
     *
     * @param materialId ID
     */
    @PreAuthorize("hasAuthority('restaurant:material:Material:remove')")
    @ApiOperation("根据ID删除材料")
    @PostMapping("/deleteMaterial")
    public void deleteMaterial(@RequestParam Long materialId) {
        this.materialService.deleteMaterial(materialId);
    }


}

